package com.hxd.otc.module.my.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.module.my.fragment.FinancialStatementsFragment;
import com.hxd.otc.module.my.fragment.StableCurrencyFragment;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.widget.TitleBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class StableCurrencyActivity extends BaseActivity {

    @BindView(R.id.tv_euc_number)
    TextView tvEucNumber;

    @BindView(R.id.title_bar)
    TitleBar titleBar;

    @BindView(R.id.tabLayout)
    TabLayout mTabLayout;

    @BindView(R.id.viewPager)
    ViewPager mViewPager;

    List<Fragment> mFragmentList;

    String[] tabNames;

    String progremme;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_stable_currency;
    }

    @Override
    protected void initView() {
        tabNames = getResources().getStringArray(R.array.array_table_currency);
        initViewPager();
    }

    public void setTvEucNumber(String str, String progremme) {
        this.progremme = progremme;
        tvEucNumber.setText(str);
    }

    private void initViewPager() {
        mFragmentList = new ArrayList<>();
        mFragmentList.add(new StableCurrencyFragment());
        mFragmentList.add(new FinancialStatementsFragment());

        mViewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragmentList.get(position);
            }

            @Override
            public int getCount() {
                return mFragmentList.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return tabNames[position];
            }
        });

        mViewPager.setOffscreenPageLimit(mFragmentList.size());
        mViewPager.setCurrentItem(0, false);
        mTabLayout.setupWithViewPager(mViewPager, true);

        setupTabIcons();

        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                TextView tabText = tab.getCustomView().findViewById(R.id.tv_bottom);
                tabText.setTextSize(16);
                tabText.setTextColor(getResources().getColor(R.color.color_text1));
                ImageView ivBotton = tab.getCustomView().findViewById(R.id.iv_botton);
                ivBotton.setVisibility(View.VISIBLE);
            }


            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                TextView tabText = tab.getCustomView().findViewById(R.id.tv_bottom);
                tabText.setTextSize(13);
                tabText.setTextColor(getResources().getColor(R.color.color_FFB1B1B1));
                ImageView ivBotton = tab.getCustomView().findViewById(R.id.iv_botton);
                ivBotton.setVisibility(View.GONE);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    private void setupTabIcons() {
        for (int i = 0; i < mFragmentList.size(); i++) {
            TabLayout.Tab tab = mTabLayout.getTabAt(i);//获得每一个tab
            tab.setCustomView(getTabView(i));//给每一个tab设置view
        }
    }

    public View getTabView(int position) {

        View view = LayoutInflater.from(this).inflate(R.layout.item_stable_currency_tab, null);
        TextView tv_bottom = view.findViewById(R.id.tv_bottom);
        tv_bottom.setText(tabNames[position]);
        tv_bottom.setTextColor(position == 0 ? getResources().getColor(R.color.color_text1) : getResources().getColor(R.color.color_FFB1B1B1));
        ImageView ivBotton = view.findViewById(R.id.iv_botton);
        if (position == 0) {
            ivBotton.setVisibility(View.VISIBLE);
            tv_bottom.setTextSize(16);
        } else {
            ivBotton.setVisibility(View.GONE);
            tv_bottom.setTextSize(13);
        }
        return view;

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {

        titleBar.setOnTitleBarItemClickListener(new TitleBar.OnTitleBarItemClickListener() {
            @Override
            public void onBack(View v) {

            }

            @Override
            public void onRight(View v) {
                if (TextUtils.isEmpty(progremme)) {
                    showToast(getString(R.string.no_case));
                    return;
                }
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString(WebViewActivity.EXTRA_TITLE, titleBar.getRightText().getText().toString());
                bundle.putString(WebViewActivity.EXTRA_URL, progremme);
                bundle.putBoolean(WebViewActivity.EXTRA_IS_PDF, true);
                bundle.putBoolean(WebViewActivity.EXTRA_IS_HELP_CENTER, false);

                intent.putExtras(bundle);
                intent.setClass(StableCurrencyActivity.this, WebViewActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }


}
